import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _class, _class2;

/* eslint-disable react/no-unused-state */
// TODO: state.content 本组件没有使用, 但是 ./index.js 中使用了

/**
 * StaticTextEditor.js
 * 本质上 TinymceEditor 是不受控的, 之前这里写错了, 一直没有更新 content 但是 编辑器里面还是有值
 * @date 2018-12-25

 */
import React from 'react';
import { Form } from 'antd';
import { Bind } from 'lodash-decorators';
import uuid from 'uuid/v4';
import RichTextEditor from "../RichTextEditor";
var StaticTextEditor = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(StaticTextEditor, _React$Component);

  var _super = _createSuper(StaticTextEditor);

  function StaticTextEditor(props) {
    var _this;

    _classCallCheck(this, StaticTextEditor);

    _this = _super.call(this, props);
    _this.staticTextEditor = React.createRef();
    _this.state = {
      editorKey: uuid(),
      content: props.content,
      // 编辑器内容
      prevContent: props.content // 保存用来比较编辑内容是否改变

    };
    return _this;
  }

  _createClass(StaticTextEditor, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var onRef = this.props.onRef;
      onRef(this);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
          editorKey = _this$state.editorKey,
          prevContent = _this$state.prevContent;
      return /*#__PURE__*/React.createElement(RichTextEditor, {
        key: editorKey,
        ref: this.staticTextEditor,
        content: prevContent,
        config: {
          height: 200,
          resize_dir: 'both',
          resize_maxWidth: 2000,
          toolbar: [{
            name: 'tooltip',
            items: ['Preview']
          }]
        },
        readOnly: true
      });
    }
  }, {
    key: "handleEditChange",
    value: function handleEditChange(dataSource) {
      this.setState({
        content: dataSource
      });
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps, prevState) {
      var content = nextProps.content;

      if (content !== prevState.prevContent) {
        return {
          editorKey: uuid(),
          content: content || '',
          prevContent: content || ''
        };
      }

      return null;
    }
  }]);

  return StaticTextEditor;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "handleEditChange", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleEditChange"), _class2.prototype)), _class2)) || _class);
export { StaticTextEditor as default };